﻿using Dapper;
using OMG.Fill.Mulberry.Model;
using OMG.Fill.Mulberry.Model.Ent;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.Linq;
namespace OMG.Fill.Mulberry.Service
{
    /// <summary>
    /// 角色功能表T_MenuRole通用操作类
    /// </summary>
    public class MenuRoleServices:MenuRoles
    {
        public Pager<List<MenuRoleEntity>> GetPageList(MenuRoleQuery query)
        {
            Pager<List<MenuRoleEntity>> obj = new Pager<List<MenuRoleEntity>>();
            var paramlist = new DynamicParameters();
            string strWhereSql = "";//from 表在下面，需完善其他查询条件在这里接的写，格式为："and xxx=@xxx空格,如下注释部分代码所示："
            //if (!string.IsNullOrEmpty(query.GroupCode))
            //{
            //  strWhereSql += "and GroupCode = @GroupCode ";
            //  paramlist.Add("GroupCode", query.GroupCode);
            //}
            string strFromSql = "from dbo.T_MenuRole where 1=1 " + strWhereSql;
            string strTotal = "select count(1)" + strFromSql + strWhereSql;
            string strOrderSql = "";//自定义完成order by语句
            string strSql = @"select * from (select *,ROW_NUMBER() over (order by Id ) as RowNumber " + strFromSql + ") as t ";
            strSql += "where t.RowNumber between  @minRow and @maxRow" + strOrderSql;
            paramlist.Add("minRow", query.MinRow);
            paramlist.Add("maxRow", query.MaxRow);
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.rows = dbCon.Query<MenuRoleEntity>(strSql,paramlist).ToList();
                    obj.total = dbCon.Query<int>(strTotal,paramlist).FirstOrDefault();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取角色功能表T_MenuRole分页列表失败", ex);
                return obj;
            }
        }


        public ResResult<MenuRoleEntity> GetModel(MenuRoleEntity menuRoleModel)
        {
            ResResult<MenuRoleEntity> obj = new ResResult<MenuRoleEntity>();
            MenuRoleModel model = new MenuRoleModel();
            string strSql = @"select * from dbo.T_MenuRole where RoleId = '" + menuRoleModel.RoleId + "' and MenuId= '" + menuRoleModel.MenuId + "'";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    if (dbCon.Query<MenuRoleEntity>(strSql) != null)
                    {
                        obj.model = dbCon.Query<MenuRoleEntity>(strSql).FirstOrDefault();
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "获取成功！";
                    }
                    else
                    {
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "获取失败！";
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取角色功能表T_MenuRole对象信息失败", ex);
                return obj;
            }
        }



        public ResResult<List<MenuRoleEntity>> GetList(string id)
        {
            ResResult<List<MenuRoleEntity>> obj = new ResResult<List<MenuRoleEntity>>();
            string strSql = @"select * from dbo.T_MenuRole where RoleId=@RoleId and ActionId != ''";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<MenuRoleEntity>(strSql, new { RoleId = id }).ToList();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新角色功能表T_MenuRole对象信息失败", ex);
                return obj;
            }
        }


        /// <summary>
        /// 添加
        /// </summary>
        public ResResult<int> AddInfo(MenuRoleModel model)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"INSERT INTO dbo.T_MenuRole";
            strSql += "(";
            strSql += "Id,";
            strSql += "MenuId,";
            strSql += "RoleId,";
            strSql += "ActionId)";
            strSql += " values(";
            strSql += "@Id,";
            strSql += "@MenuId,";
            strSql += "@RoleId,";
            strSql += "@ActionId)";
            string strDel = @"Delete from dbo.T_MenuRole where RoleId=@RoleId";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    var trans = dbCon.BeginTransaction();
                    for (int i = 0; i < model.MRList.Count; i++)
                    {
                        model.MRList[i].Id = CustomFun.GetGuid();
                    }
                    int delresult = dbCon.Execute(strDel, new { RoleId = model.RoleId }, trans);
                    int result = dbCon.Execute(strSql, model.MRList, trans);
                    //提交事务
                    trans.Commit();
                    obj.model = result;
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "添加成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("新增角色功能表T_MenuRole对象信息失败", ex);
                return obj;
            }
        }
    }
}